//****************************************************************************
//Copyright (C) 2005 Adobe Systems, Inc. All Rights Reserved.
//The following is Sample Code and is subject to all restrictions on
//such code as contained in the End User License Agreement accompanying
//this product.
//****************************************************************************

class ImageViewer {
	
	//---------------------------------------------- Properties
	
	private var container_mc:MovieClip;				// Movie clip references.
	private var target_mc:MovieClip;
	private var containerDepth:Number;				// Movie clip depths.
	private static var backgroundDepth:Number = 0;  
	private static var imageDepth:Number = 1;
	private static var maskDepth:Number = 2;
	private static var borderDepth:Number = 3;
	private var borderThickness:Number;				// Border style
	private var borderColor:Number;

	//---------------------------------------------- Methods
	
  	/**
  	 * constructor method
  	 * 
  	 * @param target				movieclip to target
  	 * @param depth				the depth of the new MC
  	 * @param x					x position of new MC
  	 * @param y					y position of new MC
  	 * @param w					width of MC
  	 * @param h					height of MC
  	 * @param borderThickness		thickness in pixels of border around image viewer
  	 * @param borderColor			the border color
  	 * @param background			whether to draw a background MC behind the image viewer
  	 */
  	public function ImageViewer (target:MovieClip, 
    	                           depth:Number, 
                               x:Number, 
                               y:Number, 
                               w:Number, 
                               h:Number,
                               borderThickness:Number,
                               borderColor:Number,
                               background:Boolean) {
	    	// Assign property values.
	    	target_mc = target;
	    	containerDepth = depth;
	    	this.borderThickness = borderThickness;
	    	this.borderColor = borderColor;
	
	    	// Set up the visual assets for this ImageViewer.
	    	buildViewer(x, y, w, h, background);
    	}

  	/**
  	 * used to build the different parts of the image viewer
  	 * 
  	 * @param x				the x position of the new viewer
  	 * @param y				the y position
  	 * @param w				the width of the new viewer
  	 * @param h 				the height of the new viewer
  	 * @param background		whether to draw a background clip
  	 */
  	private function buildViewer (x:Number, 
                                y:Number, 
                                w:Number, 
                                h:Number,
                                background:Boolean):Void {
    		createMainContainer(x, y);
      	if (background == true) {
      		createBackground(w, h);
      	}
      	createImageClip(w, h);
      	createImageClipMask(w, h);
      	createBorder(w, h);
  	}

  	/**
   	 * Creates the container that holds all the assets.
   	 * 
   	 * @param x	the x position
   	 * @param y	the y position
   	 */ 
  	private function createMainContainer (x:Number, y:Number):Void {
    		container_mc = target_mc.createEmptyMovieClip("container_mc" + containerDepth, containerDepth);
    		// Position the container clip.
    		container_mc._x = x;
    		container_mc._y = y;
  	}
	
  	/**
  	 * Creates the clip into which the image is actually loaded.
  	 *  
  	 * @param w		the width of the new clip
  	 * @oaram h		the height of the new clip 
  	 */
  	private function createBackground (w:Number, h:Number):Void {
		container_mc.createEmptyMovieClip("background_mc", backgroundDepth);
	    createMessage("loading media...");
	    // lay down a background while image loads
	  	container_mc.background_mc.lineStyle(borderThickness, borderColor);
	    container_mc.background_mc.moveTo(0, 0);
	    container_mc.background_mc.beginFill(0xFFFFFF);
	    container_mc.background_mc.lineTo(w, 0);
	    container_mc.background_mc.lineTo(w, h);
	    container_mc.background_mc.lineTo(0, h);
	    container_mc.background_mc.lineTo(0, 0);
	    container_mc.background_mc.endFill();
	}
  
  	/**
  	 * method used to set the message in the viewer (i.e. loading...)
  	 * 
  	 * @param message	i.e. loading...
  	 */
  	private function createMessage(message:String):Void {
  		// create a loading message
  		//_root.bottomText_mc.bottom_txt.text = "createMessage called";
    		container_mc.background_mc.createTextField("loading_txt", 1, 4, 4, 160, 160);
    		container_mc.background_mc.loading_txt.text = message;
    		container_mc.background_mc.loading_txt.autoSize = true;
   	 	container_mc.background_mc.loading_txt.border = false;
   	 	var messageFormat;
   	 	messageFormat = new TextFormat();
   	 	messageFormat.bold = false;
   	 	messageFormat.color = 0x777777;
   	 		container_mc.background_mc.loading_txt.setTextFormat(messageFormat);
  	}
  
  	/** 
   	 * Creates the clip into which the image is actually loaded.
   	 * 
   	 * @param w		width of the clip
   	 * @param h		height of the clip
   	 */
  	private function createImageClip (w:Number, h:Number):Void {
    		container_mc.createEmptyMovieClip("image_mc", imageDepth); 
    		// lay down a background while image loads
	  	container_mc.image_mc.lineStyle(borderThickness, borderColor);
	    container_mc.image_mc.moveTo(0, 0);
	    //container_mc.image_mc.beginFill(0xFFFFFF);  // Use blue for debugging.
	    container_mc.image_mc.lineTo(w, 0);
	    container_mc.image_mc.lineTo(w, h);
	    container_mc.image_mc.lineTo(0, h);
	    container_mc.image_mc.lineTo(0, 0);
	    container_mc.image_mc.endFill();
  	}
  
  	/**
   	 * Creates the mask over the image.
   	 * 
   	 * @param w		width of the clip
   	 * @param h 		height of the clip
   	 */
  	private function createImageClipMask (w:Number, h:Number):Void {
    		// Only create the mask if a valid width and height are specified.
    		if (!(w > 0 && h > 0)) {
      		return;
    		}

    		// In the container, create a clip to act as the mask over the image.
    		container_mc.createEmptyMovieClip("mask_mc", maskDepth);

    		// Draw a rectangle in the mask.
    		container_mc.mask_mc.moveTo(0, 0);
    		container_mc.mask_mc.beginFill(0x0000FF);  // Use blue for debugging.
    		container_mc.mask_mc.lineTo(w, 0);
    		container_mc.mask_mc.lineTo(w, h);
    		container_mc.mask_mc.lineTo(0, h);
    		container_mc.mask_mc.lineTo(0, 0);
    		container_mc.mask_mc.endFill();
  
    		// Hide the mask (it will still function as a mask when invisible).
    		// To see the mask during debugging, comment out the next line.
    		container_mc.mask_mc._visible = false; 

    		// Notice that we don't apply the mask yet. We must do that
    		// after the image starts loading, otherwise the loading of 
    		// the image will remove the mask.
  	}

  	/**
  	 * Creates the border around the image.
  	 * 
  	 * @param w		width of the clip
  	 * @param h		height of the clip
  	 */ 
  	private function createBorder (w:Number, h:Number):Void {
    		// Only create the border if a valid width and height are specified.
    		if (!(w > 0 && h > 0)) {
      		return;
    		}
	
    		// In the container, create a clip to hold the border around the image.
    		container_mc.createEmptyMovieClip("border_mc", borderDepth);
  
    		// Draw a rectangular outline in the border clip, with the
    		// specified dimensions and color.
    		container_mc.border_mc.lineStyle(borderThickness, borderColor);
    		container_mc.border_mc.moveTo(0, 0);
    		//container_mc.border_mc.beginFill(0xFFFFFF);  // Use blue for debugging.
    		container_mc.border_mc.lineTo(w, 0);
    		container_mc.border_mc.lineTo(w, h);
    		container_mc.border_mc.lineTo(0, h);
    		container_mc.border_mc.lineTo(0, 0);
    		//container_mc.border_mc.endFill();
  	}

  	/**
  	 * Loads the image.
  	 *  
  	 * @param URL	filepath to the image to load 
  	 */
  	public function loadImage (URL:String):Void {
    		// Load the JPEG file into the image_mc clip.
    		container_mc.image_mc.loadMovie(URL);
	
    		// Here comes an ugly hack. We'll clean this up
    		// when we add proper preloading support, in "Take 3":
    		// After one frame passes, the image load will have started,
    		// at which point we safely apply the mask to the image_mc clip.
    		container_mc.onEnterFrame = function ():Void {
      		this.image_mc.setMask(this.mask_mc);
      		delete this.onEnterFrame;
    		}
  	}
  
  	/** 
  	 * remove the imageViewer
  	 */
  	public function destroy():Void {
  		// remove movie clips from Stage
  		container_mc.removeMovieClip();
  	}
}